package com.coocaa.ops.admin.v1.dmp.tag;

import com.coocaa.ops.admin.base.annotation.Auth;
import com.coocaa.ops.admin.base.controller.MgrBaseController;
import com.coocaa.ops.admin.base.language.CommonLanguageProperties;
import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.admin.v1.dmp.tag.model.DmpPolicyQueryModel;
import com.coocaa.ops.admin.v1.dmp.tag.model.DmpTagQueryModel;
import com.coocaa.ops.admin.v1.dmp.tag.model.TagSaveModel;
import com.coocaa.ops.admin.v1.dmp.tag.service.IDmpTagQueryService;

import com.coocaa.ops.admin.v1.dmp.tag.service.IDmpTagSaveService;
import com.coocaa.ops.common.core.base.enums.AuthEnum;
import com.coocaa.ops.common.core.base.model.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author tangminghong
 * @date : 2019.5.18
 * @description: dmp策略-人群标签信息
 */
@RestController
@RequestMapping("/v1/dmpTag")
public class DmpTagController extends MgrBaseController {
    @Autowired
    private IDmpTagQueryService tagQueryService;
    @Autowired
    private IDmpTagSaveService dmpTagSaveService;

    private static CommonLanguageProperties commonLanguageProperties;

    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Object list(HttpServletRequest request, DmpTagQueryModel queryModel) throws Exception {
        return tagQueryService.getTagResultInfo(queryModel, WebUtil.getCurrentUserId(request));
    }

    /**
     * 人群策略-分页接口
     *
     * @param request
     * @param queryModel
     * @return
     * @throws Exception
     */
    @Auth(verifyAuthority = true, authorityType = AuthEnum.BROWSER)
    @RequestMapping(value = "/listCrowdPolicy", method = {RequestMethod.GET, RequestMethod.POST})
    public R listCrowdPolicy(HttpServletRequest request, DmpPolicyQueryModel queryModel) throws Exception {
        return tagQueryService.getCrowdPolicyPageResult(queryModel, WebUtil.getCurrentUserId(request));
    }

    @Auth(verifyAuthority = true, authorityType = AuthEnum.SAVE)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    public Object save(HttpServletRequest request, @RequestBody TagSaveModel tagSaveModel) {
        Assert.notNull(tagSaveModel, "tagSaveModel " + commonLanguageProperties.getNotNull());
        Assert.notEmpty(tagSaveModel.getDisableTagInfoList(), "disableTagInfoList holds information about the list of disabled tags" + commonLanguageProperties.getNotNull());
        dmpTagSaveService.saveDisableTag(tagSaveModel, WebUtil.getCurrentUser(request));
        return R.ok();
    }

    @Autowired
    public void setCommonLanguageProperties(CommonLanguageProperties commonLanguageProperties) {
        DmpTagController.commonLanguageProperties = commonLanguageProperties;
    }


}
